****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings

****************************************************************************
		
clear all
set more off
set matsize 800

global lotcons "female black white hispanic lunch?? lunch_miss app2 app3 anyt109 anyt110 anyt111 anyt112 anyt1lunch?? esly??"  

global nccons "math_lag read_lag science_lag math_gr_lag read_gr_lag mardgr_int_lag science_gr_lag allgr_int_lag i.lowread_nonmag#i.year lep_Y_lag ma_aig_lag rd_aig_lag aig_int_lag"


** Table 6

***** Kindergarten 

use all-lottery-movers-analysis.dta , clear 

keep if (year>2008 & ksample & stayer==1)

gen notmove1 = (1 - move1)

gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1

gen move1_lost = (1 - wonfirst)*move1

gen attappch1schl = (curr_lead1==appch1school)

gen attassschl = (curr_lead1==assschl)

gen win_complier = wonfirst*attassschl
gen win_noncomplier = wonfirst - win_complier

gen move1_win_noncomp = win_noncomplier*move1
gen move1_win_comp = win_complier*move1
gen notmove1_win_noncomp = win_noncomplier*notmove1

gen avgtestscores_at = (mathstand_at*0.5 + readstand_at*0.5)
gen avgtestscores_as = (mathstand_as*0.5 + readstand_as*0.5)
gen avgscore_diff = avgtestscores_at - avgtestscores_as

gen move1_win_noncomp_sc = move1_win_noncomp*avgscore_diff
gen move1_win_comp_sc = move1_win_comp*avgscore_diff
gen move1_lost_sc = move1_lost*avgscore_diff

tempfile ksample 
	save `ksample' 	

xtreg elem_price_resid_diff move1_lost move1_win_comp move1_win_noncomp  $lotcons , fe cluster(lottery) 
estadd local cbgcontrols = "-"
estadd local byschl = "\checkmark"
estadd local destcbgfe = "-"
estadd scalar N_ind = e(N)
est store A

xtreg  elem_price_residV2_diff move1_lost move1_win_comp move1_win_noncomp $lotcons , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd local byschl = "\checkmark"
estadd local destcbgfe = "-"
estadd scalar N_ind = e(N)
est store B

** sixth grade

use all-lottery-movers-analysis.dta , clear 

keep if (sixsample==1 & stayer==1)

gen notmove1 = (1 - move1)

gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1

gen move1_lost = (1 - wonfirst)*move1

gen attappch1schl = (curr_lead1==appch1school)

gen attassschl = (curr_lead1==assschl)

gen win_complier = wonfirst*attassschl
gen win_noncomplier = wonfirst - win_complier

gen move1_win_noncomp = win_noncomplier*move1
gen move1_win_comp = win_complier*move1
gen notmove1_win_noncomp = win_noncomplier*notmove1

gen avgtestscores_at = (mathstand_at*0.5 + readstand_at*0.5)
gen avgtestscores_as = (mathstand_as*0.5 + readstand_as*0.5)
gen avgscore_diff = avgtestscores_at - avgtestscores_as

gen move1_win_noncomp_sc = move1_win_noncomp*avgscore_diff
gen move1_win_comp_sc = move1_win_comp*avgscore_diff
gen move1_lost_sc = move1_lost*avgscore_diff

egen cbgfe = group(cbg)
egen cbgprefe = group(cbgpre)

replace midbyCBG_price_resid_diff = mid_price_resid_diff if (midbyCBG_price_resid_diff==. | move1==0)
replace midbyCBG_price_residV2_diff = mid_price_residV2_diff if (midbyCBG_price_residV2_diff==. | move1==0)

tempfile sixsample 
	save `sixsample' 
	
xtreg  midbyCBG_price_resid_diff move1_lost move1_win_comp move1_win_noncomp $lotcons $nccons , fe cluster(lottery) 
estadd local cbgcontrols = "-"
estadd local bycbg = "\checkmark" 
estadd local destcbgfe = "-"

estadd scalar N_ind = e(N)
est store C

xtreg  midbyCBG_price_residV2_diff move1_lost move1_win_comp move1_win_noncomp $lotcons $nccons , fe cluster(lottery)
estadd local bycbg = "\checkmark" 
estadd local cbgcontrols = "\checkmark" 
estadd local destcbgfe = "-"

estadd scalar N_ind = e(N)
est store D

reghdfe midbyCBG_price_residV2_diff move1_lost move1_win_comp move1_win_noncomp $lotcons $nccons , absorb(lottery cbgfe) cluster(lottery)
estadd local cbgcontrols = "\checkmark" 
estadd local bycbg = "\checkmark"
estadd local destcbgfe = "\checkmark"
estadd scalar N_ind = e(N)
est store E

esttab A B C D E using table6.tex , replace ///
mtitles("Kindergarten" "Kindergarten" "Sixth Grade" "Sixth Grade" "Sixth Grade") ///
refcat(move1_win_comp "\emph{Winners X Change HS}" , nolabel ) ///
keep(move1_lost move1_win_comp move1_win_noncomp) ///
coef(move1_lost "Lost X Change HS" move1_win_comp "Complier" move1_win_noncomp "Non-Complier") ///
label booktabs nonum gaps noobs f collabels(none) ///
star(* 0.10 ** 0.05 *** 0.01) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") ///

esttab A B C D E using table6.tex , append ///
label booktabs nonum gaps noobs f collabels(none) nomtitles plain cells(none) ///
stats(destcbgfe cbgcontrols N_ind , fmt(%1s %1s %9.0f) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"Destination CBG FE"' `"1st Stage CBG Controls"' `"Observations"'))
	
	
	
	
** Table 7

** kindergarten
use `ksample' , clear

xtreg elem_price_resid_diff move1_lost_sc move1_win_noncomp_sc  $lotcons , fe cluster(lottery) 
estadd local cbgcontrols = "-"
estadd local byschl = "\checkmark"
estadd local destcbgfe = "-"
estadd scalar N_ind = e(N)
est store A

xtreg  elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd local byschl = "\checkmark"
estadd local destcbgfe = "-"
estadd scalar N_ind = e(N)
est store B

** sixth grade
use `sixsample' , clear 

xtreg  midbyCBG_price_resid_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons , fe cluster(lottery) 
estadd local cbgcontrols = "-"
estadd local bycbg = "\checkmark" 
estadd local destcbgfe = "-"
estadd scalar N_ind = e(N)
est store C

xtreg  midbyCBG_price_residV2_diff move1_lost_sc move1_win_comp_sc move1_win_noncomp_sc $lotcons $nccons , fe cluster(lottery)
estadd local bycbg = "\checkmark" 
estadd local cbgcontrols = "\checkmark" 
estadd local destcbgfe = "-"
estadd scalar N_ind = e(N)
est store D

reghdfe midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons , absorb(lottery cbgfe) cluster(lottery)
estadd local cbgcontrols = "\checkmark" 
estadd local bycbg = "\checkmark"
estadd local destcbgfe = "\checkmark"
estadd scalar N_ind = e(N)
est store E

esttab A B C D E using table7.tex , replace ///
mtitles("Kindergarten" "Kindergarten" "Sixth Grade" "Sixth Grade" "Sixth Grade") ///
refcat(move1_lost_sc "\emph{Change HS X Score Diff}" , nolabel ) ///
keep(move1_lost_sc move1_win_noncomp_sc) ///
coef(move1_lost_sc "Lost" move1_win_noncomp_sc "Won X Non-Complier") ///
label booktabs nonum gaps noobs f collabels(none) ///
star(* 0.10 ** 0.05 *** 0.01) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") ///

esttab A B C D E using table7.tex , append ///
label booktabs nonum gaps noobs f collabels(none) nomtitles plain cells(none) ///
stats(destcbgfe cbgcontrols N_ind , fmt(%1s %1s %9.0f) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"Destination CBG FE"' `"1st Stage CBG Controls"' `"Observations"'))



** Table A15

** kindergarten
use `ksample' , clear

xtreg elem_price_resid_ch_hsld move1_lost move1_win_comp move1_win_noncomp  $lotcons , fe cluster(lottery) 
estadd local cbgcontrols = "-"
estadd local byschl = "\checkmark"
estadd local destcbgfe = "-"
estadd scalar N_ind = e(N)
est store A

xtreg  elem_price_residV2_ch_hsld move1_lost move1_win_comp move1_win_noncomp $lotcons , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd local byschl = "\checkmark"
estadd local destcbgfe = "-"
estadd scalar N_ind = e(N)
est store B

use `sixsample' , clear

xtreg midbyCBG_price_resid_ch_hsld move1_lost move1_win_comp move1_win_noncomp  $lotcons $nccons , fe cluster(lottery) 
estadd local cbgcontrols = "-"
estadd local byschl = "\checkmark"
estadd local destcbgfe = "-"
estadd scalar N_ind = e(N)
est store C

xtreg  midbyCBG_price_residV2_ch_hsld move1_lost move1_win_comp move1_win_noncomp $lotcons $nccons , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd local byschl = "\checkmark"
estadd local destcbgfe = "-"
estadd scalar N_ind = e(N)
est store D

reghdfe midbyCBG_price_residV2_ch_hsld move1_lost move1_win_comp move1_win_noncomp $lotcons $nccons , absorb(lottery cbgfe) cluster(lottery)
estadd local cbgcontrols = "\checkmark" 
estadd local bycbg = "\checkmark"
estadd local destcbgfe = "\checkmark"
estadd scalar N_ind = e(N)
est store E

esttab A B C D E using tableA15.tex , replace ///
mtitles("Kindergarten" "Kindergarten" "Sixth Grade" "Sixth Grade" "Sixth Grade") ///
refcat(move1_win_comp "\emph{Winners X Change HS}" , nolabel ) ///
keep(move1_lost move1_win_comp move1_win_noncomp) ///
coef(move1_lost "Lost X Change HS" move1_win_comp "Complier" move1_win_noncomp "Non-Complier") ///
label booktabs nonum gaps noobs f collabels(none) ///
star(* 0.10 ** 0.05 *** 0.01) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") ///

esttab A B C D E using tableA15.tex , append ///
label booktabs nonum gaps noobs f collabels(none) nomtitles plain cells(none) ///
stats(destcbgfe cbgcontrols N_ind , fmt(%1s %1s %9.0f) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"Destination CBG FE"' `"1st Stage CBG Controls"' `"Observations"'))





** Table A16

** kindergarten
use `ksample' , clear

xtreg elem_price_resid_ch2_hsld move1_lost move1_win_comp move1_win_noncomp  $lotcons , fe cluster(lottery) 
estadd local cbgcontrols = "-"
estadd local byschl = "\checkmark"
estadd local destcbgfe = "-"
estadd scalar N_ind = e(N)
est store A

xtreg  elem_price_residV2_ch2_hsld move1_lost move1_win_comp move1_win_noncomp $lotcons , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd local byschl = "\checkmark"
estadd local destcbgfe = "-"
estadd scalar N_ind = e(N)
est store B

use `sixsample' , clear 

xtreg midbyCBG_price_resid_ch2_hsld move1_lost move1_win_comp move1_win_noncomp  $lotcons $nccons , fe cluster(lottery) 
estadd local cbgcontrols = "-"
estadd local byschl = "\checkmark"
estadd local destcbgfe = "-"
estadd scalar N_ind = e(N)
est store C

xtreg  midbyCBG_price_residV2_ch2_hsld move1_lost move1_win_comp move1_win_noncomp $lotcons $nccons , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd local byschl = "\checkmark"
estadd local destcbgfe = "-"
estadd scalar N_ind = e(N)
est store D

reghdfe midbyCBG_price_residV2_ch2_hsld move1_lost move1_win_comp move1_win_noncomp $lotcons $nccons , absorb(lottery cbgfe) cluster(lottery)
estadd local cbgcontrols = "\checkmark" 
estadd local bycbg = "\checkmark"
estadd local destcbgfe = "\checkmark"
estadd scalar N_ind = e(N)
est store E

esttab A B C D E using tableA16.tex , replace ///
mtitles("Kindergarten" "Kindergarten" "Sixth Grade" "Sixth Grade" "Sixth Grade") ///
refcat(move1_win_comp "\emph{Winners X Change HS}" , nolabel ) ///
keep(move1_lost move1_win_comp move1_win_noncomp) ///
coef(move1_lost "Lost X Change HS" move1_win_comp "Complier" move1_win_noncomp "Non-Complier") ///
label booktabs nonum gaps noobs f collabels(none) ///
star(* 0.10 ** 0.05 *** 0.01) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") ///

esttab A B C D E using tableA16.tex , append ///
label booktabs nonum gaps noobs f collabels(none) nomtitles plain cells(none) ///
stats(destcbgfe cbgcontrols N_ind , fmt(%1s %1s %9.0f) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"Destination CBG FE"' `"1st Stage CBG Controls"' `"Observations"'))



	
